/**
 * 项目申请类型定义
 * 配合project_schema.js使用，定义不同方案类型项目的申请流程和状态
 */

// 项目申请类型常量定义
const APPLY_TYPES = {
    // 基础类型
    NEW_PROJECT: 1,      // 新项目申请
    ITERATION: 2,        // 迭代项目
    CUSTOM: 3,           // 客户定制
    RESEARCH: 4,         // 技术预研
    
    // 方案类型
    HALL_SENSOR: 10,     // 霍尔传感器
    MAGNETIC_ENCODER: 11, // 磁编传感器
    MOTOR_CONTROL: 12,   // 电机控制
    VACUUM: 13,          // 吸尘器
    FAN: 14,             // 风机
    PUMP: 15,            // 水泵
    OTHER: 99            // 其他类型
};

// 申请类型名称映射
const APPLY_TYPE_NAMES = {
    [APPLY_TYPES.NEW_PROJECT]: '新项目申请',
    [APPLY_TYPES.ITERATION]: '迭代项目',
    [APPLY_TYPES.CUSTOM]: '客户定制',
    [APPLY_TYPES.RESEARCH]: '技术预研',
    
    [APPLY_TYPES.HALL_SENSOR]: '霍尔传感器',
    [APPLY_TYPES.MAGNETIC_ENCODER]: '磁编传感器',
    [APPLY_TYPES.MOTOR_CONTROL]: '电机控制',
    [APPLY_TYPES.VACUUM]: '吸尘器',
    [APPLY_TYPES.FAN]: '风机',
    [APPLY_TYPES.PUMP]: '水泵',
    [APPLY_TYPES.OTHER]: '其他类型'
};

// 项目状态定义
const PROJECT_STATUS = {
    PLANNING: 10,       // 规划中
    DESIGN: 20,         // 设计中
    DEVELOPMENT: 30,    // 开发中
    TESTING: 40,        // 测试中
    REVIEW: 50,         // 评审中
    COMPLETED: 60,      // 已完成
    CANCELLED: 99       // 已取消
};

// 项目状态名称
const PROJECT_STATUS_NAMES = {
    [PROJECT_STATUS.PLANNING]: '规划中',
    [PROJECT_STATUS.DESIGN]: '设计中',
    [PROJECT_STATUS.DEVELOPMENT]: '开发中',
    [PROJECT_STATUS.TESTING]: '测试中',
    [PROJECT_STATUS.REVIEW]: '评审中',
    [PROJECT_STATUS.COMPLETED]: '已完成',
    [PROJECT_STATUS.CANCELLED]: '已取消'
};

// 活动状态定义
const ACTIVE_STATUS = {
    // 规划阶段
    PLAN_DRAFT: 11,            // 草稿
    PLAN_REVIEW: 12,           // 评审中
    PLAN_APPROVED: 13,         // 已批准

    // 设计阶段
    DESIGN_DRAFT: 21,          // 草稿
    DESIGN_REVIEW: 22,         // 评审中
    DESIGN_APPROVED: 23,       // 已批准

    // 开发阶段
    DEV_ONGOING: 31,           // 进行中
    DEV_REVIEW: 32,            // 评审中
    DEV_COMPLETED: 33,         // 已完成

    // 测试阶段
    TEST_ONGOING: 41,          // 进行中
    TEST_FAILED: 42,           // 未通过
    TEST_PASSED: 43,           // 已通过

    // 评审阶段
    REVIEW_ONGOING: 51,        // 进行中
    REVIEW_FAILED: 52,         // 未通过
    REVIEW_PASSED: 53,         // 已通过

    // 完成阶段
    COMPLETED: 60              // 已完成
};

// 活动状态名称
const ACTIVE_STATUS_NAMES = {
    [ACTIVE_STATUS.PLAN_DRAFT]: '草稿',
    [ACTIVE_STATUS.PLAN_REVIEW]: '评审中',
    [ACTIVE_STATUS.PLAN_APPROVED]: '已批准',
    
    [ACTIVE_STATUS.DESIGN_DRAFT]: '草稿',
    [ACTIVE_STATUS.DESIGN_REVIEW]: '评审中',
    [ACTIVE_STATUS.DESIGN_APPROVED]: '已批准',
    
    [ACTIVE_STATUS.DEV_ONGOING]: '进行中',
    [ACTIVE_STATUS.DEV_REVIEW]: '评审中',
    [ACTIVE_STATUS.DEV_COMPLETED]: '已完成',
    
    [ACTIVE_STATUS.TEST_ONGOING]: '进行中',
    [ACTIVE_STATUS.TEST_FAILED]: '未通过',
    [ACTIVE_STATUS.TEST_PASSED]: '已通过',
    
    [ACTIVE_STATUS.REVIEW_ONGOING]: '进行中',
    [ACTIVE_STATUS.REVIEW_FAILED]: '未通过',
    [ACTIVE_STATUS.REVIEW_PASSED]: '已通过',
    
    [ACTIVE_STATUS.COMPLETED]: '已完成'
};

// 项目事件定义
const EVENTS = {
    SUBMIT_FOR_APPROVAL: 'submit_for_approval',   // 提交审批
    APPROVE: 'approve',                           // 审批通过
    REJECT: 'reject',                             // 审批拒绝
    CANCEL: 'cancel',                             // 取消项目
    START_DESIGN: 'start_design',                 // 开始设计
    COMPLETE_DESIGN: 'complete_design',           // 完成设计
    START_DEVELOPMENT: 'start_development',       // 开始开发
    COMPLETE_DEVELOPMENT: 'complete_development', // 完成开发
    START_TESTING: 'start_testing',               // 开始测试
    COMPLETE_TESTING: 'complete_testing',         // 完成测试
    SLEEP_REQUEST: 'sleep_request',               // 休眠申请
    WAKEUP_REQUEST: 'wakeup_request',             // 唤醒申请
    COMPLETE_PROJECT: 'complete_project'          // 完成项目
};

// 项目事件名称
const EVENT_NAMES = {
    [EVENTS.SUBMIT_FOR_APPROVAL]: '提交审批',
    [EVENTS.APPROVE]: '审批通过',
    [EVENTS.REJECT]: '审批拒绝',
    [EVENTS.CANCEL]: '取消项目',
    [EVENTS.START_DESIGN]: '开始设计',
    [EVENTS.COMPLETE_DESIGN]: '完成设计',
    [EVENTS.START_DEVELOPMENT]: '开始开发',
    [EVENTS.COMPLETE_DEVELOPMENT]: '完成开发',
    [EVENTS.START_TESTING]: '开始测试',
    [EVENTS.COMPLETE_TESTING]: '完成测试',
    [EVENTS.SLEEP_REQUEST]: '休眠申请',
    [EVENTS.WAKEUP_REQUEST]: '唤醒申请',
    [EVENTS.COMPLETE_PROJECT]: '完成项目'
};

// 方案类型与申请类型的映射关系
const SOLUTION_TYPE_TO_APPLY_TYPE = {
    '霍尔': APPLY_TYPES.HALL_SENSOR,
    '磁编': APPLY_TYPES.MAGNETIC_ENCODER,
    '电机控制': APPLY_TYPES.MOTOR_CONTROL,
    '吸尘器': APPLY_TYPES.VACUUM,
    '风机': APPLY_TYPES.FAN,
    '水泵': APPLY_TYPES.PUMP,
    '其他': APPLY_TYPES.OTHER
};

// 根据方案类型获取申请类型
function getApplyTypeByProjectType(projectType) {
    return SOLUTION_TYPE_TO_APPLY_TYPE[projectType] || APPLY_TYPES.OTHER;
}

// 根据申请类型获取方案类型
function getProjectTypeByApplyType(applyType) {
    for (const [key, value] of Object.entries(SOLUTION_TYPE_TO_APPLY_TYPE)) {
        if (value === applyType) {
            return key;
        }
    }
    return '其他';
}

// 根据方案类型获取默认的状态流转路径
function getDefaultStatusFlowByType(projectType) {
    // 基础流程适用于所有项目类型
    const baseFlow = [
        {
            status: PROJECT_STATUS.PLANNING,
            activeStatus: ACTIVE_STATUS.PLAN_DRAFT,
            nextEvent: EVENTS.SUBMIT_FOR_APPROVAL,
            nextStatus: PROJECT_STATUS.PLANNING,
            nextActiveStatus: ACTIVE_STATUS.PLAN_REVIEW
        },
        {
            status: PROJECT_STATUS.PLANNING,
            activeStatus: ACTIVE_STATUS.PLAN_REVIEW,
            nextEvent: EVENTS.APPROVE,
            nextStatus: PROJECT_STATUS.DESIGN,
            nextActiveStatus: ACTIVE_STATUS.DESIGN_DRAFT
        },
        {
            status: PROJECT_STATUS.DESIGN,
            activeStatus: ACTIVE_STATUS.DESIGN_DRAFT,
            nextEvent: EVENTS.COMPLETE_DESIGN,
            nextStatus: PROJECT_STATUS.DEVELOPMENT,
            nextActiveStatus: ACTIVE_STATUS.DEV_ONGOING
        },
        {
            status: PROJECT_STATUS.DEVELOPMENT,
            activeStatus: ACTIVE_STATUS.DEV_ONGOING,
            nextEvent: EVENTS.COMPLETE_DEVELOPMENT,
            nextStatus: PROJECT_STATUS.TESTING,
            nextActiveStatus: ACTIVE_STATUS.TEST_ONGOING
        },
        {
            status: PROJECT_STATUS.TESTING,
            activeStatus: ACTIVE_STATUS.TEST_ONGOING,
            nextEvent: EVENTS.COMPLETE_TESTING,
            nextStatus: PROJECT_STATUS.REVIEW,
            nextActiveStatus: ACTIVE_STATUS.REVIEW_ONGOING
        },
        {
            status: PROJECT_STATUS.REVIEW,
            activeStatus: ACTIVE_STATUS.REVIEW_ONGOING,
            nextEvent: EVENTS.APPROVE,
            nextStatus: PROJECT_STATUS.COMPLETED,
            nextActiveStatus: ACTIVE_STATUS.COMPLETED
        }
    ];
    
    // 针对不同项目类型的特殊流程
    const typeSpecificFlow = {
        '霍尔': [...baseFlow],
        '磁编': [...baseFlow],
        '电机控制': [...baseFlow],
        '吸尘器': [...baseFlow],
        '风机': [...baseFlow],
        '水泵': [...baseFlow],
        '其他': [...baseFlow]
    };
    
    return typeSpecificFlow[projectType] || baseFlow;
}

// 导出模块
module.exports = {
    APPLY_TYPES,
    APPLY_TYPE_NAMES,
    PROJECT_STATUS,
    PROJECT_STATUS_NAMES,
    ACTIVE_STATUS,
    ACTIVE_STATUS_NAMES,
    EVENTS,
    EVENT_NAMES,
    SOLUTION_TYPE_TO_APPLY_TYPE,
    getApplyTypeByProjectType,
    getProjectTypeByApplyType,
    getDefaultStatusFlowByType
}; 